package threadPool;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolGetTest {
    /**
     * 使用remove方法
     * 已经正在运行中的任务不可以删除
     * execute方法提交的，未运行的任务可以删除
     * submit方法提交的，未运行的任务不可以删除，小心踩坑
     */
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,2,100, TimeUnit.SECONDS
        ,new LinkedBlockingDeque<>());
        Runnable run = new RunnerDemo("A");
        Runnable run2 = new RunnerDemo("B");
        Runnable run3 = new RunnerDemo("c");
        Runnable run4 = new RunnerDemo("d");
        executor.execute(run);
        executor.execute(run2);
        executor.execute(run3);
        executor.execute(run4);
        Thread.sleep(1000);
        System.out.println("返回核心线程数 getCorePoolSize= " + executor.getCorePoolSize());
        System.out.println("返回线程数  getPoolSize=" + executor.getPoolSize());
        System.out.println("返回最大允许线程数  getMaximumPoolSize=" + executor.getMaximumPoolSize());
        System.out.println("返回池中同时存在的最大线程数  getLargestPoolSize=" + executor.getLargestPoolSize());
        System.out.println("预定指定的任务总和  getTaskCount=" + executor.getTaskCount());
        System.out.println("当前线程池已经完成的任务数  getCompletedTaskCount=" + executor.getCompletedTaskCount());
        System.out.println("正在执行任务线程的大致数目  getActiveCount=" + executor.getActiveCount());
        System.out.println("空闲时间  getKeepAliveTime=" + executor.getKeepAliveTime(TimeUnit.SECONDS));
        Thread.sleep(1000);

        System.out.println("-------------------------------------------------");
        System.out.println("返回核心线程数 getCorePoolSize= " + executor.getCorePoolSize());
        System.out.println("返回线程数  getPoolSize=" + executor.getPoolSize());
        System.out.println("返回最大允许线程数  getMaximumPoolSize=" + executor.getMaximumPoolSize());
        System.out.println("返回池中同时存在的最大线程数  getLargestPoolSize=" + executor.getLargestPoolSize());
        System.out.println("预定指定的任务总和  getTaskCount=" + executor.getTaskCount());
        System.out.println("当前线程池已经完成的任务数  getCompletedTaskCount=" + executor.getCompletedTaskCount());
        System.out.println("正在执行任务线程的大致数目  getActiveCount=" + executor.getActiveCount());
        System.out.println("空闲时间  getKeepAliveTime=" + executor.getKeepAliveTime(TimeUnit.SECONDS));
    }
}
class RunnerDemo implements Runnable{
    private String name;

    public RunnerDemo(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("System.currentTimeMillis() = " + System.currentTimeMillis());

    }
}